package com.lntea.mybatisDemo.domain;

import java.io.Serializable;

/**
 * 
 * 
 * @author luhuipan
 */
//
public class Pager implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6968849530423001154L;
	private static final int PAGE_SIZE_DEF = 10;
	private static final int BTN_SIZE = 6;
	private int pageId = 1;
	private int rowCount = 0;
	private int pageSize = PAGE_SIZE_DEF;
	private int pageCount = 0;
	private int pageOffset = 0;
	private int pageTail = 0;
	private String orderField;
	private boolean orderDirection;

	private int length = BTN_SIZE;

	private int startIndex = 0;

	private int endIndex = 0;

	private int[] indexs;

	/**
	 * @return length
	 */
	public int getLength() {
		return length;
	}

	/**
	 * @param length
	 *            set length
	 */
	public void setLength(int length) {
		this.length = length;
	}

	/**
	 * @return indexs
	 */
	public int[] getIndexs() {
		int len = getEndIndex() - getStartIndex() + 1;
		indexs = new int[len];
		for (int i = 0; i < len; i++) {
			indexs[i] = (getStartIndex() + i);
		}
		return indexs;
	}

	/**
	 * @param indexs
	 *            set indexs
	 */
	public void setIndexs(int[] indexs) {
		this.indexs = indexs;
	}

	/**
	 * @return startIndex
	 */
	public int getStartIndex() {
		startIndex = pageId - (length / 2);
		if (startIndex < 1) {
			startIndex = 1;
		}
		return startIndex;
	}

	/**
	 * @param startIndex
	 *            set startIndex
	 */
	public void setStartIndex(int startIndex) {
		this.startIndex = startIndex;
	}

	/**
	 * @return endIndex
	 */
	public int getEndIndex() {
		if (getStartIndex() < 1) {
			setStartIndex(1);
		}
		endIndex = (getStartIndex() + length) <= getPageCount() ? (getStartIndex() + length)
				: getPageCount();
		return endIndex;
	}

	/**
	 * @param endIndex
	 *            set endIndex
	 */
	public void setEndIndex(int endIndex) {
		this.endIndex = endIndex;
	}

	/**
	 * 
	 */
	public Pager() {
		this.orderDirection = true;
	}

	/**
	 * 
	 */
	protected void doPage() {
		this.pageCount = this.rowCount / this.pageSize + 1;

		if ((this.rowCount % this.pageSize == 0) && pageCount > 1)
			this.pageCount--;

		this.pageOffset = (this.pageId - 1) * this.pageSize;
		this.pageTail = this.pageOffset + this.pageSize;
		if ((this.pageOffset + this.pageSize) > this.rowCount)
			this.pageTail = this.rowCount;
	}

	/**
	 * @return condition
	 */
	public String getOrderCondition() {
		String condition = "";
		if (this.orderField != null && this.orderField.length() != 0) {
			condition = " order by " + orderField
					+ (orderDirection ? " " : " desc ");
		}
		return condition;
	}

	/**
	 * @return condition
	 */
	public String getMysqlQueryCondition() {
		String condition = "";
		condition = " limit " + pageOffset + "," + pageSize;
		return condition;
	}

	/**
	 * @return pagingSelect
	 */
	public String getOrclPreQueryCondition() {
		String pagingSelect = "";
		if (pageOffset > 0) {
			pagingSelect = "select * from ( select row_.*, rownum rownum_ from ( ";
		} else {
			pagingSelect = "select * from ( ";
		}
		return pagingSelect;
	}

	/**
	 * @return pagingSelect
	 */
	public String getOrclPostQueryCondition() {
		String pagingSelect = "";
		if (pageOffset > 0) {
			pagingSelect = " ) row_ ) where rownum_ <= " + pageTail
					+ " and rownum_ > " + pageOffset;
		} else {
			pagingSelect = " ) where rownum <= " + pageTail;
		}
		return pagingSelect;
	}

	/**
	 * @param orderDirection
	 *            set orderDirection
	 */
	public void setOrderDirection(boolean orderDirection) {
		this.orderDirection = orderDirection;
	}

	/**
	 * @return orderDirection
	 */
	public boolean isOrderDirection() {
		return orderDirection;
	}

	/**
	 * @param orderField
	 *            set orderField
	 */
	public void setOrderField(String orderField) {
		this.orderField = orderField;
	}

	/**
	 * @return orderField
	 */
	public String getOrderField() {
		return orderField;
	}

	/**
	 * @param pageCount
	 *            set pageCount
	 */
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	/**
	 * @return pageCount
	 */
	public int getPageCount() {
		return pageCount;
	}

	/**
	 * @param pageId
	 *            set pageId
	 */
	public void setPageId(int pageId) {
		this.pageId = pageId;
	}

	/**
	 * @return pageId
	 */
	public int getPageId() {
		return pageId;
	}

	/**
	 * @param pageOffset
	 *            set pageOffset
	 */
	public void setPageOffset(int pageOffset) {
		this.pageOffset = pageOffset;
	}

	/**
	 * @return pageOffset
	 */
	public int getPageOffset() {
		return pageOffset;
	}

	/**
	 * @param pageSize
	 *            set pageSize
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * @return pageSize
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * @param pageTail
	 *            set pageTail
	 */
	public void setPageTail(int pageTail) {
		this.pageTail = pageTail;
	}

	/**
	 * @return pageTail
	 */
	public int getPageTail() {
		return pageTail;
	}

	/**
	 * @param rowCount
	 *            set rowCount
	 */
	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
		this.doPage();
	}

	/**
	 * @return indexs
	 */
	public int getRowCount() {
		return rowCount;
	}

}